package com.museum.data;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;

import com.museum.model.Event;
import com.museum.model.Location;
import com.museum.model.Mp_Places_News;
import com.museum.model.Mp_User_Watched;
import com.museum.model.Open_Date;
import com.museum.model.Open_Date_Item;
import com.museum.model.Place;

public class Mp_User_Watched_Handler {
	Mp_User_Watched mpuserwatched;
	private Place place;
	private Place place_info;
	private Event event;
	private Mp_Places_News place_news;
	XmlRpc xmlrpc;
	private String _username;
	private String _password;
	public Mp_User_Watched_Handler(String username, String password){
		_username = username;
		_password = password;
		xmlrpc = new XmlRpc();
		mpuserwatched = new Mp_User_Watched();
		this.Parse();
	}
	public Mp_User_Watched getMp_User_Watched(){
		return mpuserwatched;
	}
	private void Parse(){
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("username", _username);
      	params.put("password", _password); 
      	
      	Map<String, Object> response = xmlrpc.call("mp.user.watched", params);
      	for (Map.Entry<String, Object> entry : response.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();	 
            
            //Log.d("TAG","key: " +key+ ", value: " + value.toString());

            Map<String, Object> response2 = (Map<String, Object>)value;
            for (Map.Entry<String, Object> entry2 : response2.entrySet()) {
            	
            	String key2 = entry2.getKey();
            	
                if(key2.equals("place_news")){                      	
                    Object [] value2 = (Object[])entry2.getValue();
                    for(int i=0;i<value2.length;i++){
                    	this.ParsePlace_News(value2[i]);
                    	//Log.d("TAG","key: " +key2+ ", value2: " + value2[i].toString());
                    }
                 }else if(key2.equals("place")){  
                    Object value2 = entry2.getValue();
                    place = new Place();    	            	
    	            this.ParsePlace(value2);
                 }
                 
            }
      	}
	}
	private void ParsePlace(Object obj){
		Map<String, Object> response3 = (Map<String, Object>)obj;
        for (Map.Entry<String, Object> entry3 : response3.entrySet()) {
        	String key3 = entry3.getKey();
            Object value3 = entry3.getValue();
            
            if(key3.equals("place_id")){
            	place.setPlaceId(value3.toString());
            }
            else if(key3.equals("email")){
            	place.setEmail(value3.toString());
            }else if(key3.equals("website")){
            	place.setWebsite(value3.toString());
            }else if(key3.equals("added_timestamp")){
            	place.setAdded_timestamp(value3.toString());
            }else if(key3.equals("description")){
            	place.setDescription(value3.toString());
            }else if(key3.equals("name")){
            	place.setName(value3.toString());
	      	}else if(key3.equals("create_timestamp")){
	      		place.setCreate_timestamp(value3.toString());
	        }else if(key3.equals("exhibition_status")){
	        	place.setExhibition_status(value3.toString());
	        }
	        else if(key3.equals("location")){
	        	Location location = new Location();
	            Map<String, Object> response4 = (Map<String, Object>)value3;
			    for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
			         String key4 = entry4.getKey();
				     Object value4 = entry4.getValue();								         
				        
				     if(key4.equals("longitude")){
				    	 location.setLongitude(value4.toString());
				     }else if(key4.equals("description")){
				        location.setDescription(value4.toString());
				     }else if(key4.equals("latitude")){
				        location.setLatitude(value4.toString());
				     }								        
			    }//End Loop4
			    place.setLocation(location);
	        }else if(key3.equals("open_date")){
	        	Open_Date opendate = new Open_Date();
	        	Map<String, Object> response4 = (Map<String, Object>)value3;
			        for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
			           	String key4 = entry4.getKey();
				        Object value4 = entry4.getValue();
				        
				        Open_Date_Item opendateitem = new Open_Date_Item();
				        opendateitem.setDate(key4);
					    Map<String, Object> response5 = (Map<String, Object>)value4;
						for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
							String key5 = entry5.getKey();
							Object value5 = entry5.getValue();									        
								        
							if(key5.equals("close_time")){
								opendateitem.setClose_time(value5.toString());
							}else if(key5.equals("open_time")){
								opendateitem.setOpen_time(value5.toString());
							}							
						}//End Loop5
						opendate.addItem(opendateitem);
						place.setOpen_date(opendate);
			        }//End Loop4
	          }//End if
        }//End Loop3
        mpuserwatched.setPlace(place);
	}
	private void ParsePlace_News(Object obj){
		place_news = new Mp_Places_News();	
		event = new Event();
		Map<String, Object> response3 = (Map<String, Object>)obj;
		for (Map.Entry<String, Object> entry3 : response3.entrySet()) {
        	String key3 = entry3.getKey();
            Object value3 = entry3.getValue();
            
            //Log.d("TAG","Value3: " + value3.toString());
            
            if(key3.equals("event_id")){
            	event.setEvent_Id(value3.toString());
            }else if(key3.equals("display_name")){
            	event.setDisplay_Name(value3.toString());
            }else if(key3.equals("description")){
            	event.setDescription(value3.toString());
            }else if(key3.equals("start_date")){
            	event.setStart_Date(value3.toString());
            }else if(key3.equals("end_date")){
            	event.setEnd_Date(value3.toString());
	      	}else if(key3.equals("event_type")){
	      		event.setEvent_Type(value3.toString());
	        }else if(key3.equals("flee")){
	        	event.setFlee(value3.toString());
	        }else if(key3.equals("web")){
				event.setWeb_Site(value3.toString());
			}else if(key3.equals("email")){
				event.setEmail(value3.toString());
			}else if(key3.equals("phone_number")){
				event.setPhone_Number(value3.toString());
			}else if(key3.equals("place_info")){
				place_info = new Place();
				Map<String, Object> response4 = (Map<String, Object>)value3;
		        for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
		        	String key4 = entry4.getKey();
		            Object value4 = entry4.getValue();
		            
		            if(key4.equals("place_id")){
		            	place_info.setPlaceId(value4.toString());
		            }else if(key4.equals("email")){
		            	place_info.setEmail(value4.toString());
		            }else if(key4.equals("website")){
		            	place_info.setWebsite(value4.toString());
		            }else if(key4.equals("added_timestamp")){
		            	place_info.setAdded_timestamp(value4.toString());
		            }else if(key4.equals("description")){
		            	place_info.setDescription(value4.toString());
		            }else if(key4.equals("name")){
		            	place_info.setName(value4.toString());
			      	}else if(key4.equals("create_timestamp")){
			      		place_info.setCreate_timestamp(value4.toString());
			        }else if(key4.equals("exhibition_status")){
			        	place_info.setExhibition_status(value4.toString());
			        }
			        else if(key4.equals("location")){
			        	Location location = new Location();
			            Map<String, Object> response5 = (Map<String, Object>)value4;
					    for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
					         String key5 = entry5.getKey();
						     Object value5 = entry5.getValue();								         
						        
						     if(key5.equals("longitude")){
						    	 location.setLongitude(value5.toString());
						     }else if(key5.equals("description")){
						        location.setDescription(value5.toString());
						     }else if(key5.equals("latitude")){
						        location.setLatitude(value5.toString());
						     }								        
					    }//End Loop4
					    place_info.setLocation(location);
			        }else if(key4.equals("open_date")){
			        	Open_Date opendate = new Open_Date();
			        	Map<String, Object> response5 = (Map<String, Object>)value4;
					        for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
					           	String key5 = entry5.getKey();
						        Object value5 = entry5.getValue();
						        
						        Open_Date_Item opendateitem = new Open_Date_Item();
						        opendateitem.setDate(key5);
							    Map<String, Object> response6 = (Map<String, Object>)value5;
								for (Map.Entry<String, Object> entry6 : response6.entrySet()) {
									String key6 = entry6.getKey();
									Object value6 = entry6.getValue();									        
										        
									if(key6.equals("close_time")){
										opendateitem.setClose_time(value6.toString());
									}else if(key6.equals("open_time")){
										opendateitem.setOpen_time(value6.toString());
									}							
								}//End Loop5
								opendate.addItem(opendateitem);
								place_info.setOpen_date(opendate);
					        }//End Loop4
			          }//End if			          
		        }//End Loop3
		        event.setPlace_Info(place_info);
            }         
        }//End Loop3
        place_news.addItem(event);
        mpuserwatched.setPlace_News(place_news);
	}
}
